<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/19
 * Time: 10:37
 */
namespace Admin\Controller;
use Admin\Controller\CommonController;

class CPCController extends CommonController{
    public function index(){
        $cpc = $this->dg(M("cpc")->order("`cname`")->select());
        $this->assign("cpc",$cpc);
        $this->display();
    }
    public function add(){
        if(!IS_POST){
            $cpc = $this->dg(M("cpc")->order("`cname`")->select());
            $this->assign("pid",$_GET['pid']);
            $this->assign("cpc",$cpc);
            $this->display();
        }else{
            $post=I("post.");

            $insert =M("cpc")->data($post)->add();
            if($insert){
                $this->success("操作成功",__MODULE__.'/CPC/index');
            }else{
                $this->error("操作失败");
            }
        }
    }
    public function edit(){
        if(IS_POST) {
            $id=$_GET['nid'];
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/CPC/index');
            }else{
                $data=I("post.");
                $test=M("cpc")->data($data)->where("id={$id}")->save();
                if($test) {
                    $this->success('操作成功！',__MODULE__.'/CPC/index');
                } else {
                    $this->error("操作失败");
                }

            }
        }else{
            $id=$_GET["nid"];
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'//index');
            }else{
                $arr=M("cpc")->where("id={$id}")->select();
                $cpc = $this->dg(M("cpc")->order("`cname`")->select());
                $class =M("cpc")->field('classy')->group("classy")->select();
                $cla=[];
                foreach($class as $val){
                    if($val['classy']==0){
                        $val['class']='渠道';
                    }elseif($val['classy']==1) {
                        $val['class'] = '分区';
                    }elseif($val['classy']==2) {
                        $val['class'] = '区域';
                    }else{
                        continue;
                    }
                    $cla[]=$val;
                }
                $this->assign("cla",$cla);
                $this->assign("cpc",$cpc);
                $this->assign("arr",$arr);
                $this->display();

            }
        }
    }
    public function del(){
        $nid = intval($_GET['nid']);
        if(empty($nid)) {
            $this->error('异常操作！',__MODULE__.'/CPC/index');
        }
        if(M("cpc")->where("id = {$nid}")->delete() !== fasle) {
            $this->success('操作成功！',__MODULE__.'/CPC/index');
        } else {
            $this->error('操作失败！',__MODULE__.'/CPC/index');
        }
    }
    public function dg($data,$pid=0,$empty="&nbsp;&nbsp;"){
        $arr = [];
        foreach($data as $val){
            if($val['pid']==$pid){
                $val['cname']=$empty.$val['cname'];
                $arr[]=$val;
                foreach($this->dg($data,$val['id'],$empty."|--&nbsp;&nbsp;")as $v){
                    $arr[]=$v;
                }
            }
        }
        return $arr;
    }
    public function _empty(){
        $this->error("404");
    }
}